package com.study.stream.jack;

import com.alibaba.fastjson.JSON;
import com.study.vo.StudyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用自定义的通信信道 JackSource 实现消息的发送
 */
@Slf4j
@EnableBinding(JackSource.class)
public class JackSendService {

    @Autowired
    private JackSource jackSource;

    //使用自定义的输出信道发送消息
    public void sendMessage(StudyMessage message){

        String messageStr = JSON.toJSONString(message);
        log.info("JackSendService send message: [{}]", messageStr);
        jackSource.jackOutput().send(
                MessageBuilder.withPayload(messageStr).build()
        );
    }
}
